package invs.invs300both;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;


public class CardHisActivity extends Activity implements OnClickListener{
	
	CardRecordDbHelper m_Recdb = null; 
	Cursor m_RecCursor = null;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_card_his); 

        Button btn = (Button)findViewById(R.id.firstpage);
        btn.setOnClickListener(this); 
        btn = (Button)findViewById(R.id.prepage);
        btn.setOnClickListener(this);  
        btn = (Button)findViewById(R.id.nextpage);
        btn.setOnClickListener(this);          
        btn = (Button)findViewById(R.id.lastpage);
        btn.setOnClickListener(this);  
        
        
        m_Recdb = new CardRecordDbHelper(this);  
        m_Recdb.GetRecordCount();
        ShowPage(0);

        EditText edit = (EditText)findViewById(R.id.curpage);
        edit.setOnKeyListener(new OnKeyListener() {			
			public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
				if(arg1 == KeyEvent.KEYCODE_ENTER) {				
					EditText edit = (EditText)findViewById(R.id.curpage);
					ShowPage(Integer.parseInt(edit.getText().toString()));
				}
				return false;
			}
		}); 
    };
    
    void ShowPage(int iPage) {
        ListView lv = (ListView) this.findViewById(R.id.listView);   
        if (m_RecCursor != null)
        	m_RecCursor.close();

        m_RecCursor = m_Recdb.query(iPage);  
        
        lv.setAdapter(new CursorAdapter(this, m_RecCursor));     
        
        lv.setOnItemClickListener(new OnItemClickListener(){   
            @Override   
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {   
            	if (m_Recdb == null)
            		return;
                int i = m_Recdb.m_iCurPage*m_Recdb.m_iRecPerPage + arg2+1; 
                
                Intent intent = new Intent(CardHisActivity.this, CardRecDtlActivity.class);
    			Bundle bundle = new Bundle();
    			bundle.putInt("rownum", i);
    			bundle.putInt("total", m_Recdb.m_iTotalRec);

                intent.putExtras(bundle);
                CardHisActivity.this.startActivity(intent);    
            }   
               
        });
        
        Button btn;
        EditText edit;
        TextView txt;
        
        if ( m_Recdb.m_iTotalPage == 0){
            btn = (Button)findViewById(R.id.nextpage);
            btn.setEnabled(false);         
            btn = (Button)findViewById(R.id.lastpage);
            btn.setEnabled(false);          	
            btn = (Button)findViewById(R.id.firstpage);
            btn.setEnabled(false);         
            btn = (Button)findViewById(R.id.prepage);
            btn.setEnabled(false);    
            edit = (EditText)findViewById(R.id.curpage);
            edit.setEnabled(false);
        	edit.setText("0");
        	txt = (TextView)findViewById(R.id.totalpage);
        	txt.setText("共0页");  
        	edit.setFocusable(false);
        	return ;
        }
        
        if (m_Recdb.m_iCurPage == m_Recdb.m_iTotalPage-1 ){
            btn = (Button)findViewById(R.id.nextpage);
            btn.setEnabled(false);         
            btn = (Button)findViewById(R.id.lastpage);
            btn.setEnabled(false);            	
        }
        
        if (m_Recdb.m_iCurPage == 0){
            btn = (Button)findViewById(R.id.firstpage);
            btn.setEnabled(false);         
            btn = (Button)findViewById(R.id.prepage);
            btn.setEnabled(false);            	
        }
        
        if (m_Recdb.m_iCurPage > 0 && m_Recdb.m_iTotalPage != 0){
            btn = (Button)findViewById(R.id.firstpage);
            btn.setEnabled(true);         
            btn = (Button)findViewById(R.id.prepage);
            btn.setEnabled(true);          	
        }
        
        if (m_Recdb.m_iCurPage < m_Recdb.m_iTotalPage-1 && m_Recdb.m_iTotalPage != 0){
            btn = (Button)findViewById(R.id.nextpage);
            btn.setEnabled(true);         
            btn = (Button)findViewById(R.id.lastpage);
            btn.setEnabled(true);            	
        }  
        	
        txt = (TextView)findViewById(R.id.totalpage);
        txt.setText("共" + m_Recdb.m_iTotalPage + "页");
        
        edit = (EditText)findViewById(R.id.curpage);
        if (m_Recdb.m_iCurPage == 0 && m_Recdb.m_iTotalPage == 0){
        	edit.setText("0");
        	txt.setText("共0页");
        }else{
        	edit.setText("" + (m_Recdb.m_iCurPage + 1));
        }   
        edit.setCursorVisible(false);
    }
    
    public class CursorAdapter extends BaseAdapter {  		 		  
		private Context context;  		  
		private Cursor cursor;  		  
		private LayoutInflater inflater;  		  
		private LinearLayout pageLayout;  
		  
		public CursorAdapter(Context context, Cursor cursor) {  
			super();  
			this.context = context;  
			this.cursor = cursor;  
			inflater = LayoutInflater.from(context);  			
		}  
		  
		public int getCount() {  
			return cursor.getCount();  
		}  
		  
		public Object getItem(int position) {  
			return position;  
		}  
		  
		public long getItemId(int position) {  
			return position;  
		}  
 
		public View getView(int position, View convertView, ViewGroup parent) {  		  
			cursor.moveToPosition(position);  
			RelativeLayout pageLayout = (RelativeLayout) inflater.inflate(R.layout.cardrecorditem, null);  	
			  
			TextView txt;
			ImageView iv;
			  
			txt = (TextView) pageLayout.findViewById(R.id.rownum);  
			//txt.setText(cursor.getString(cursor.getColumnIndex("rowid")));
			int i = m_Recdb.m_iCurPage*m_Recdb.m_iRecPerPage + position + 1;
			txt.setText("" + i);
			
			txt = (TextView) pageLayout.findViewById(R.id.idnum);  
			txt.setText(cursor.getString(cursor.getColumnIndex("idnum")));
			  
			txt = (TextView) pageLayout.findViewById(R.id.name);  
			txt.setText(cursor.getString(cursor.getColumnIndex("name")));
			  
			txt = (TextView) pageLayout.findViewById(R.id.sex);  
			txt.setText(cursor.getString(cursor.getColumnIndex("sex")));
			  
			txt = (TextView) pageLayout.findViewById(R.id.nation);  
			txt.setText(cursor.getString(cursor.getColumnIndex("nation")));
			//txt.setText("�ɹ�");
			  
			iv = (ImageView) pageLayout.findViewById(R.id.zp);  
			byte[] jpgData = cursor.getBlob(cursor.getColumnIndex("jpg"));
			try
			{
			Bitmap jpg = BitmapFactory.decodeByteArray(jpgData, 0, jpgData.length);
			iv.setImageBitmap(jpg);	
			}catch(Exception e)
			{
				
			}
					  
			return pageLayout; 			 		 
		}    
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch(arg0.getId()){
		case R.id.firstpage:	
			ShowPage(0);
			break;		
		case R.id.prepage:	
			PrePage();
			break;
		case R.id.nextpage:
			NextPage();
			break;	
		case R.id.lastpage:	
			ShowPage(50000);
			break;			
		}
		
	}
	
	void PrePage() {
		if (m_RecCursor != null){
			ShowPage(m_Recdb.m_iCurPage-1);
		}else{
			ShowPage(0);
		}
	}
		
	void NextPage() {
		if (m_RecCursor != null){
			ShowPage(m_Recdb.m_iCurPage+1);
		}else{
			ShowPage(50000);
		}
	}
	
	protected void onDestroy() {
		 super.onDestroy();
		 m_Recdb.close();
	}
        
}
